package se;

import java.util.HashMap;
import java.util.Scanner;

/**
 * Author: hu
 * Date: 2021/11/23 14:31
 */
public class GouWu {
    HashMap map = new HashMap();
    Scanner sc = new Scanner(System.in);
    double total = 0;

    public GouWu(){
        map.put("dba","123");
    }
    public static void main(String[] args) {
    GouWu gw = new GouWu();
    gw.XiaoWei();
    }

    /**
     *  Author: hu
     *  Date  : 2021/11/23 14:34
     *登录
     */
    public  boolean login() {
        System.out.println("---用户登录---");

        int count = 0;
        while (count < 2) {
            System.out.println("请输入用户名：");
            Scanner sc = new Scanner(System.in);
            String user = sc.next();
            System.out.println("请输入密码：");
            String pwd = sc.next();
            System.err.println(user+":"+pwd+":"+map.get(user));
            if (pwd.equals(map.get(user))) {
                System.out.println("---登录成功---");
                return true;
            } else {
                System.out.println("用户或密码错误");
                count++;
            }
        }
        System.out.println("连续2次登录失败,退出");
        return false;
    }

    /**
     *  Author: hu
     *  Date  : 2021/11/23 14:35
     *注册
     */

    public void register(){
            System.out.println("---注册用户---");
            System.out.println("请输入注册用户名：");
            String user = sc.next();
            System.out.println("请输入密码：");
            String pwd = sc.next();
            map.put(user,pwd);
            System.out.println("注册成功！请登录后继续使用");
        }

    public void shopping(){
        int a=0;
        int b=0;
        while(true){
            System.out.println("请选择购买商品：1苹果 ￥12， 2西瓜 ￥24, 0结束购物");
            int num = sc.nextInt();

            if (num==1){
                total+=12;
                a++;
                System.out.println("已购买苹果"+a);
            }else if(num==2){
                total+=24;
                b++;
                System.out.println("已购买西瓜"+b);
            }else if(num==0){
                System.out.println("本次购物苹果"+a+"个，西瓜"+b+"个共计花费"+total+"元");
                break;
            }else{
                System.out.println("输入参数不合法，请重新输入");

            }
        }

    }

    public void XiaoWei() {
        System.out.println("欢迎使用小微购物平台");
        while(true) {
            System.out.println("1注册  2登录  3退出平台");
            int a = sc.nextInt();
            if (a == 1) {
                register();
            } else if (a == 2) {
                boolean xw = login();
                if (xw) shopping();
            } else if (a==3){
                System.out.println("退出小微平台");
                break;
            }else{
                System.out.println("输入参数不合法");
            }
        }
    }
}
